﻿// =============================================================================
// TNTTools Attributes.cs
// Copyright (c) 2013 TNTTechnologies Inc.
// =============================================================================

#region

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Serialization;

#endregion

namespace TNTTools.Reflection{
    public class Attributes : List<AttributeInfo>{
        public bool ContainsIgnore = false;

        public Attributes(PropertyInfo info){
            this.Init(info.GetCustomAttributes(true));
        }

        public Attributes(object[] tempclassAttributes){
            this.Init(tempclassAttributes);
        }

        private void Init(object[] tempclassAttributes){
            for (int i = 0; i < tempclassAttributes.Length; ++i){
                if (tempclassAttributes[i] is XmlAttributeAttribute){
                    this.Add(
                        new AttributeInfo(){
                            value = ((XmlAttributeAttribute)tempclassAttributes[i]).AttributeName,
                            attributeTypeName = ((XmlAttributeAttribute)tempclassAttributes[i]).TypeId.ToString()
                        });
                } else if (tempclassAttributes[i] is XmlRootAttribute){
                    this.Add(
                        new AttributeInfo(){
                            attributeTypeName = tempclassAttributes[i].GetType().Name,
                            value = ((XmlRootAttribute)tempclassAttributes[i]).ElementName
                        });
                } else if (tempclassAttributes[i] is XmlArrayAttribute){
                    this.Add(
                        new AttributeInfo(){
                            attributeTypeName = tempclassAttributes[i].GetType().Name,
                            value = ((XmlArrayAttribute)tempclassAttributes[i]).ElementName
                        });
                } else{
                    this.Add(new AttributeInfo(){ attributeTypeName = tempclassAttributes[i].GetType().Name });
                }
            }
        }

        public new virtual void Add(AttributeInfo obj){
            if (obj.attributeTypeName == typeof(XmlIgnoreAttribute).Name){
                this.ContainsIgnore = true;
                return;
            }
            base.Add(obj);
        }

        public string FindValue(ref int startIndex, string typeName){
            int count = this.Count;
            for (int i = 0; i < count; ++i){
                if (this[i].attributeTypeName == typeName){
                    startIndex = i;
                    return this[i].value;
                }
            }
            startIndex = -1;
            return String.Empty;
        }

        public string GetXmlRootAttributeValue(ref int startIndex){
            return this.FindValue(ref startIndex, "XmlRootAttribute");
        }

        public string GetXmlArrayAttributeValue(ref int startIndex){
            return this.FindValue(ref startIndex, "XmlArrayAttribute");
        }

        public string GetXmlAttributeAttribute(ref int startIndex){
            return this.FindValue(ref startIndex, "System.Xml.Serialization.XmlAttributeAttribute");
        }
    }
}